PHP Array Functions

    PHP supports many array functions that enable developers to manipulate arrays. PHP arrays allow you to store, manage and operate on variables. You can use either a single dimensional or multidimensional array that can be created by the developer or by any function. This feature comes with PHP and there is no need to make any configuration changes to use and define arrays. Below is the list of PHP arrays functions that you can use-

    1. array()

    You can create an indexed or associative array using this function which can be single or multi-dimensional.

    Syntax- 1.1 Syntax for PHP indexed arrays:

    $a = array(val1, val2, val3, ...)

    1.2 Syntax for PHP associative arrays:

    $a = array(key1 => val1, key2 => val2...)
    • Creating empty array
    <?php
     $arr = array();
     print_r($arr);
    ?>

    Output- Array ()

    1.3 Creating PHP indexed array with elements

    <?php
     $abc = array("L", "R", "C");
     print_r($abc);
    ?>

    Output-

    Array
    (
     [0] => L
     [1] => R
     [2] => C
    )

    1.4 PHP associative array-

    Example-

    <?php
     $arr = array(1 => "H", 2 => "Y", 3 => "T");
     print_r($arr);
    ?>

    Output-

    Array
    (
     [1] => H
     [2] => Y
     [3] => T
    )

    2. array_change_key_case()

    This function allows you to change the case of the provided keys of the passed array and return the keys either in lower case or upper case depending upon the option that is passed within the function. Syntax-

    array array_change_key_case ( array $input [, int $case] )

    Where the input is the array name that is mandatory to be passed for which we want to change the case. The case will specify to which case you want to change the keys, if not specified then it will convert the keys in lower case. Example-

    <?php
     $arr = array("FirSt"=> 10, "SecOnd" => 400, );
     print_r(array_change_key_case($arr, CASE_UPPER));
    ?>

    Output-

    Array
    (
     [FIRST] => 10
     [SECOND] => 400
    )

    Example-

    <?php
     $arr = array("FirSt"=> 10,);
     print_r(array_change_key_case($arr, CASE_LOWER));
    ?>

    Output

    Array
    (
     [first] => 10
    )

    3. array_chunk()

    This function is used to divide the passed array into smaller chunks that is based on the value provided within the function. The size of the last chunk may be the remaining values of the array. Syntax-

    array array_chunk ( array $input, int $size [, bool $preserve_keys] );

    Where the input is the name of the array that is needed to be passed and is a mandatory field. The size specifies the size of each chunk and is mandatory to be passed to define how we are going to split our array. The preserve_keys is an optional part if it is set to true then the passed array will be preserved so its default value is always false. Example-

    <?php
     $input_demo = array('abc', 'bcd', 'cde');
     print_r(array_chunk($input_demo, 2));
    ?>

    Output-

    Array
    (
     [0] => Array
     (
     [0] => abc
     [1] => bcd
     )
     [1] => Array
     (
     [0] => cde
     )
    )
    

    4. array_column()

    This function will provide you with the value from the single column of the passed array which is identified by the column_key. Syntax

    array array_column( array $input , mixed $column_key [, mixed $index_key = NULL ] )

    Where the input is the array name for which we want the column values, the column_key represent which column value you want to retrieve and is represented in an integer value for indexed array and a string value for an associative array and the last parameter is the index_key which is optional and allow the column to use as the keys for the returned array. Example-

    <?php
     $records = array(
     array(
     'id' => 2135,
     'first_name' => ‘Jacob’,
     'last_name' => 'Ali',
     ),
     array(
     'id' => 3245,
     'first_name' => ‘Sam’,,
     'last_name' => 'Mac',
    )
     );
     $first_names = array_column($records, 'first_name');
     print_r($first_names);
    ?>
    

    Output-

    Array
    (
     [0] => Jacob
     [1] => Sam
    )

    5. array_combine()-

    This function allows you to combine two same or different arrays but having the same number of elements and results in creating a new array from the keys array as keys and taking the values from values array. Syntax-

    array array_combine ( array $keys, array $values );

    Where the keys represent the first array to be passed whose values are used as keys to creating the new array. The values represent the second array whose values will be used to create the values of the new array. Example-

    <?php
     $a = array(‘hello, ‘bye’);
     $b = array(‘Sam’, ‘Joker’);
     $c = array_combine($a, $b);
     print_r($c);
    ?>

    Output-

    Array
    (
     [hello] => Sam
     [bye] => Joker
    )

    6. array_count_values()

    This function will return an associative array that will use the values of the provided array as its key and the values are specified by the frequency in the provided array. Syntax-

    array array_count_values ( array $input );

    Where the input array defines the values that we want to count. Example-

    <?php
     $arr = array("hello", "bye", "hi", "hello" );
     print_r(array_count_values($arr));
    ?>

    Output-

    Array
    (
     [hello] => 2
     [hi] => 1
     [bye] => 1
    )

    7. array_diff()

    This function can take any number of arrays and it will check the values of the first array with the second provided array and it will return the values from the first array which is not present in the second or others array. Syntax-

    array array_diff ( array $array1, array $array2 [, array $array3 ...] );

    Where the first two arrays passed to the function is mandatory to provide you with the result. Example-

    <?php
     $array1 = array("hi", "bye");
     $array2 = array("hi", "hello");
     print_r(array_diff($array1, $array2));
    ?>

    Output-

    Array 
    ( 
     [1] => hi
    )

    8. array_diff_assoc()

    This function works exactly the same as the array_diff the only difference is that this function works on associative arrays. The array_diff_assoc() function will check the keys and values of the two or more arrays and return the values from the first array which is not present in the second or other arrays. Syntax-

    array array_diff_assoc( array $array1, array $array2 [, array $array3...] );

    Where the first two arrays are mandatory to be passed to produce the result. Example-

    <?php
     $arr1 = array( "a"=>"hi", "b"=>"bye");
     $arr2 = array( "a"=>"hi", "b"=>"hello");
     print_r(array_diff_assoc($arr1, $arr2));
    ?>

    Output-

    Array
    (
     [b] => bye
    )

    9. array_diff_ukey()

    This function is used to compare the arrays on the basis of their keys with the help of user-defined functions that will return the array that will have the values from array one. This function is somehow different from the array_diff() as they compare only the values of the given array and array_diff_assoc() compares the arrays on the basis of key/value but array_diff_ukey uses user-defined functions to do the comparison. Syntax-

    array_diff_ukey ( $array1, $array2 [, $array3...,callback $key_compare_func] );

    Where the first two arrays are important and mandatory for comparison and the key_compare_func is a callback function which will compare the keys and return an integer. Example-

    <?php
     function key_compare_func($a, $b) {
     if ($a === $b) {
     return 0;
     }
     return ($a > $b)? 1: -1;
     }
     $arr1 = array(0=>"banana", 1=>"orange", 2=>"grapes");
     $arr2 = array(3=>"apple",1=>"apricot", 5=>"mango");
     print_r(array_diff_ukey($arr1,$arr2,"key_compare_func"));
    ?>
    

    Output-

    Array
    (
     [0] => banana
     [2] => grapes
    )

    10. array_fill()

    This function will use num values to fill an array as the value and uses index_parameter to start the key value. Syntax-

    array array_fill ( int $start_index, int $num, mixed $value );

    Example-

    <?php
     $arr1 = array_fill(5, 2, hi);
     print_r($arr1);
    ?>

    Output-

    Array (
     [5] => hi
     [6] => hi
     )

    11. array_fill_keys()

    This function will use the value parameter as a value to fill up an array and use the keys array value as the key. Syntax-

    array array_fill_keys ( array $keys, mixed $value );

    Example-

    <?php
     $ = array('f1', 5);
     $arr = array_fill_keys($input, hi);
     print_r($arr)
    ?>

    Output-

    Array (
     [f1] => hi
     [5] => hi
    )

    12. array_filter()

    This function will iterate over the values of the passed array and then return these values to the callback function. Syntax-

    array array_filter ( array $input [, callback $callback] );

    Example-

    <?php
     function odd($var) {
     return($var & 1);
     }
     $input1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
     $input2 = array(6, 7, 8, 9, 10, 11, 12);
     echo "Odd Values:\n";
     print_r(array_filter($input1, "odd"));
    ?>

    Output-

    Odd Values:
    Array (
     [a] => 1
     [c] => 3
     [e] => 5 
    )

    13. array_intersect()

    This function will compare array values and returns the matches Example-

    <?php
     $arr1 = array("a" => "hi", "Maruthi", "bye");
     $arr2 = array("b" => "hi", "yellow", "hello");
     $val = array_intersect($arr1, $arr2);
     print_r($val);
    ?>

    Output-

    Array (
     [a] => hi
     [0] => hello
    )

    14. array_intersect_assoc()

    This function will compare the array keys and values which will return the matches. Example-

    <?php
     $arr1 = array("a" => "hi", "tell", "bye");
     $arr2 = array("a" => "hi", "say", "hello");
     $var = array_intersect_assoc($arr1, $arr2);
     print_r($var);
    ?>

    Output-

    Array ( [a] => hi)

    15. array_intersect_key()

    This function will compare the array keys, and then returns the matches Example-

    <?php
     $arr1 = array('hi' => 1, 'red' => 2, ‘bye’ => 3 );
     $arr2 = array(‘bye’ => 4, ‘hi’=> 5, 'pink' => 6,);
     $var = array_intersect_key($arr1, $arr2);
     print_r($var);
    ?>

    Output- Array ( [hi] => 1 [bye] => 3 )

    16. array_intersect_uassoc()

    This function will compare the array keys and values, with an user-made function check, and then returns the matches Syntax-

    array_intersect_uassoc($array1, $array2 [, $array3 ..., callback $key_compare_func] );

    Example-

    <?php
     $arr1 = array("a" => "hi", "b" => "bye", "c" => "hello", "say");
     $arr2 = array("a" => "HI", "B" => "bye", "tell", "HELLO");
     $var = array_intersect_uassoc($arr1, $arr2, "strcasecmp");
     print_r($var);
    ?>

    Output-

    Array ( [b] => bye)

    17. array_intersect_ukey()

    This function will compare the array keys only , with an additional user-made function check, and returns the matches unlike array_intersect_uassoc() function. Syntax-

    array_intersect_ukey ( $array1, $array2 [, $array3..., callback $key_compare_func] );

    Example-

    <?php
     function key_compare_func($a1, $a2) {
     if ($a1 == $a2)
     return 0;
     else if ($a1 > $a2)
     return 1;
     else
     return -1;
     }
     $arr1 = array(‘b’=>1, ‘hi’=>2, ‘g’=>3, ‘do’=>4);
     $arr2 = array(‘g’=>5, ‘b’=>6, ‘say’=>7, ‘does’=>8);
     $var = array_intersect_ukey($arr1, $arr2, "key_compare_func");
     var_dump($var);
    ?>

    Output-

    array(2) {
     ["b"]=>
     int(1)
     ["g"]=>
     int(3)
    }

    18. array_key_exists() This function will check if the specified key exists in the array or not. Syntax-

    bool array_key_exists ( $key, $array );

    Example-

    <?php
     $arr = array(‘hi’ => 10, ‘bye’=> 400);
     if (array_key_exists('first', $arr)) {
     echo "element is in the array";
     }
    ?>

    Output- element is in the array 19. array_keys() This function will returns all the keys of an array Syntax-

    array_keys ( $input [, $search_value [, $strict]] );

    Example-

    <?php
     $arr = array("a"=>"hi","b"=>"bye","c"=>"hello");
     print_r(array_keys($arr));
     $var = array("a"=>"hi","b"=>"bye","c"=>"hello");
     print_r(array_keys($var,"hello"));
     $val = array(10,20,30,"10");
     print_r(array_keys($val,"10",false));
    ?>

    Output-

    Array ( [0] => a [1] => b [2] => c ) Array ( [0] => c ) Array ( [0] => 0 [1] => 3 )

    20. array_map() This function will send each value of an array to a user-defined function, which will return the new values Syntax-

    array array_map ( callback $callback, array $array1 [, array $array2...] );

    Example-

    <?php
     function call_back_func($var1, $var2) {
     if ($v1 === $v2) {
     return "equal";
     }
     return "different";
     }
     $arr1 = array(1, 2, 3, 4);
     $arr2 = array(10, 2, 30, 4);
     $res = array_map("call_back_func", $arr1, $arr2);
     print_r($res);
    ?>

    Output-

    Array ( [0] => different [1] => equal [2] => different [3] => equal )

    21. array_merge() This function will merge one or more arrays into one array. Syntax-

    array array_merge ( array $array1 [, array $array2 [, array $array3...]] );

    Example-

    <?php
     $arr1 = array("a"=>"Horse","b"=>"Cat");
     $arr2 = array("d"=>"Cow");
     print_r(array_merge($arr1,$arr2));
    ?>

    Output-

    Array ( [a] => Horse [b] => Cat [d] => Cow )

    22. array_merge_recursive()

    This function will merge one or more arrays into one array recursively. Syntax-

    array array_merge_recursive ( array $array1 [, array $array2...] )

    Example-

    <?php
     $arr1 = array("a"=>"hi","b"=>"bye");
     $arr2 = array("d"=>"bye","a"=>"hello");
     print_r(array_merge_recursive($arr1,$arr2));
    ?>

    Output-

    Array ( [a] => Array ( [0] => hi [1] => hello ) [b] => bye [d] => bye )

    23. array_multisort()

    This function will allow you to sort the multiple or multi-dimensional arrays Syntax-

    array_multisort(array1,sorting order,sorting type,array2...);

    Example-

    <?php
     $arr1 = array("40", 20, 100, "a");
     $arr2 = array(1, 6, "5", 1);
     array_multisort($arr1, $arr2);
     print_r($arr1);
     print_r($arr2);
    ?>

    Output-

    Array ( [0] => 20 [1] => 40 [2] => a [3] => 100 ) Array ( [0] => 6 [1] => 1 [2] => 1 [3] => 5 )

    24. array_pad() This function will insert a specified number of items to an array with a specified value. Syntax-

    array_pad ( $array, $pad_size, $pad_value );

    Example-

    <?php
     $arr = array("a"=>"hi","b"=>"bye","c"=>"hello");
     print_r(array_pad($arr,7, "HYE"));
    ?>

    Output-

    Array ( [a] => hi [b] => bye [c] => hello [0] => HYE )

    25. array_pop()

    This function will allow you to delete the last element of a given array Syntax-

    array_pop ( $array );

    Example-

    <?php
     $arr = array("a"=>"hello","b"=>"bye","c"=>"hi");
     print_r(array_pop($ar));
     print_r("\n");
     print_r(array_pop($arr));
    ?>

    Output-

    hi
    bye

    26. array_product()

    This function will allow you to calculate the product of the values given in an array Syntax-

    array_product ( $array );

    Example-

    <?php
     $arr = array(1,2,3);
     print_r(array_product($arr));
    ?>

    Output- 6

    27. array_push()

    This function will allow you to insert one or more elements to the end of a given array Syntax-

    array_push ( $array, $var1 [, $var2...] );

    Example-

    <?php
     $arr = array("a"=>"hi","b"=>"bye","c"=>"hello");
     print_r(array_push($arr, "say"));
     print_r("\n");
     print_r($arr );
    ?>

    Output-

    4 Array ( [a] => hi [b] => bye [c] => hello [0] => say)

    28. array_rand()

    This function will allow you to return one or more random keys from an array Syntax-

    array_rand ( $input [, $num_req] );

    Example-

    <?php
     $arr = array("a"=>"hi","b"=>"hello","c"=>"bye");
     print_r(array_rand($arr));
    ?>

    Output- b

    29. array_reduce()

    This function will allow you to return an array as a string, using a user-defined function Syntax-

    array_reduce ( $array, callback $function [, int $initial] );

    Example-

    <?php
     function call_back_function($var1,$var2) {
     return $var1 . "-" . $var2;
     }
     $val = array("a"=>"hi","b"=>"bye","c"=>"hello");
     print_r(array_reduce($val, call_back_function));
     print_r("<br />");
     print_r(array_reduce($val, call_back_function, 10));
    ?>

    Output-

    -hi-bye-hello
    10-hi-bye-hello

    30. array_reverse()

    This function will allow you to return an array in the reverse order Syntax-

    array_reverse ( $array [, $preserve_keys] );

    Example-

    <?php
     $arr = array("a"=>"hi","b"=>"hello","c"=>"bye");
     print_r(array_reverse($arr));
    ?>

    Output-

    Array ( [c] => bye [b] => hello [a] => hi )

    31. array_search()

    This function will allow you to search an array for a given value and then returns the key Syntax-

    array_search($value, $array [,$strict]);

    Example-

    <?php
     $arr = array("a"=>"hi","b"=>"hello","c"=>"bye");
     print_r(array_search("bye", $arr));
    ?>

    Output- c

    32. array_shift()

    This function will allow you to remove the first element from an array, and then returns the value of the removed element Syntax-

    array_shift ( $array );

    Example-

    <?php 
     $arr = array("a"=>"hi","b"=>"hello","c"=>"bye");
     print_r(array_shift($arr));
     print_r("\n");
     print_r(array_shift($arr));
    ?>

    Output- hi hello 33. array_slice() This function will allow you to return the selected parts of an array Syntax-

    array_slice($array, $offset [,$length [,$preserve_keys]] );

    Example-

    <?php
     $arr = array("a", "b", "c", "d", "e");
     print_r(array_slice($arr, 2, -1));
     print_r(array_slice($arr, 2, -1, true));
    ?>

    Output-

    Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d )

    34. array_splice()

    This function will allow you to remove and replace the specified elements of an array Syntax-

    array_splice ( $input, $offset [,$length [,$replacement]] );

    Example-

    <?php
     $arr = array("ab", "bc", "cd");
     array_splice($arr, 2);
     print_r($arr);
     print_r("<br />");
     $arr = array("dg", "rt", "th", "yd");
     array_splice($arr, 1, -1);
     print_r($input);
     print_r("<br />");
    ?>

    Output-

    Array ( [0] => ab [1] => bc )
    Array ( [0] => dg [1] => yd )

    35. array_sum()

    This function will allow you to return the sum of the values in an array Syntax-

    array_sum ( $array );

    Example-

    <?php
     $arr = array(2, 4, 6, 8);
     echo "sum(arr) = " . array_sum($arr);
     print "\n" ;
     $arr1 = array("a" => 2.1, "b" => 5.6, "c" => 3.8);
     echo "sum(arr1) = " . array_sum($arr1);
    ?>

    Output-

    sum(arr) = 20 sum(arr1) = 11.5

    36. array_udiff()

    This function will allow you to compare an array value in a user-made function and then returns an array with the value that is present in array 1 but not in the array 2. Syntax-

    array_udiff( $array1, $array2 [, $array3 ..., $data_compare_func] );

    Example-

    <?php
     function call_back_function($var1,$var2) {
     if ($var1 === $var2) {
     return 0;
     }
     return 1;
     }
     $arr1 = array("a"=>"orange","b"=>"mango","c"=>"banana");
     $arr2 = array("a"=>"orange","b"=>"mango","c"=>"apple");
     print_r(array_udiff($arr1,$arr2,"call_back_function"));
    ?>

    Output-

    Array ( [c] => tell )

    37. array_udiff_assoc()

    This function will allow you to compare the array keys, and compares array values in a user-made function, and returns an array with the values present in the first array but not in the other array. Syntax-

    array_udiff_assoc ( $array1, $array2 [, $array3 ..., $data_compare_func] );

    Example-

    <?php
     function call_back_function($var1,$var2) {
     if ($var1 === $var2) {
     return 0;
     }
     return 1;
     }
     $arr1 = array("a"=>"hi","b"=>"bye","c"=>"tell");
     $arr2 = array("a"=>"hi","b"=>"go","c"=>"say");
     print_r(array_udiff_assoc($arr1,$arr2,"call_back_function"));
    ?>

    Output-

    Array ( [b] => bye [c] => tell )

    38. array_udiff_uassoc()

    This function will allow you to compare the array keys and their values in user-defined functions, and then returns an array Syntax-

    array_udiff_uassoc ( $array1, $array2 [, $array3 ..., $func1, $func2] );

    Example-

    <?php
     function func1($var1,$var2) {
     if ($var1 === $var2) {
     return 0;
     }
     return 1;
     }
     function func2($var1,$var2) {
     if ($var1 === $var2) {
     return 0;
     }
     return 1;
     }
     $arr1 = array("a"=>"hi","b"=>"bye","c"=>"tell");
     $arr2 = array("a"=>"hi","b"=>"go","c"=>"say");
     print_r(array_udiff_uassoc($arr1,$arr2,"func1", "func2"));
    ?>

    Output-

    Array ( [b] => bye [c] => tell )

    39. array_uintersect()

    This function will allow you to compare the array values in a user-defined function and then returns an array Syntax-

    array_uintersect ( $array1, $array2 [, $array3 ..., $data_compare_func] );

    Example

    <?php
     $arr1 = array("a"=>"g", "b"=>"bye", "c"=>"hi", "hello");
     $arr2 = array("a"=>"G", "B"=>"bye", "hi", "hello");
     print_r(array_uintersect($arr1, $arr2, "strcasecmp"));
    ?> 

    Output

    Array ( [a] => g [b] => bye [c] => hi [0] => hello )

    40. array_uintersect_assoc()

    This function will allow you to compare the array keys, and compares array values in a user-defined function, and then returns an array Syntax-

    array_uintersect_assoc( $array1, $array2 [, $array3 ..., $data_compare_func] );

    Example-

    <?php
     $arr1 = array("a"=>"g", "b"=>"bye", "c"=>"hi", "hello");
     $arr2 = array("a"=>"G", "B"=>"bye", "hi", "hello");
     print_r(array_uintersect_assoc($arr1, $arr2, "strcasecmp"));
    ?> 

    Output-

    Array ( [a] => g )

    41. array_uintersect_uassoc()

    This function will allow you to compare the array keys and array values in user-made functions, and returns an array Syntax-

    array_uintersect_assoc( $array1, $array2 [, $array3 ..., $func1], $func2 );

    Example-

    <?php
     $arr1 = array("a"=>"g", "b"=>"bye", "c"=>"hi", "hello");
     $arr2 = array("a"=>"G", "B"=>"bye", "hi", "hello");
     print_r(array_uintersect_uassoc($arr1, $arr2, "strcasecmp", “strcasecmp”));
    ?> 

    Output-

    Array ( [a] => g [b] => bye )

    42. array_unique()

    This function will allow you to remove the duplicate values from an array Syntax-

    array_unique ( $array );

    Example-

    <?php
     $arr1 = array("a"=>"g", "b"=>"bye", "c"=>"hi", "hello");
     $res = array_unique($arr1);
     print_r($res);
    ?> 

    Output-

    Array ( [a] => g [b] => bye [c] => hi [0] => hello )

    43. array_unshift()

    This function will allow you to add one or more elements to the beginning of a given array Syntax-

    array_unshift($array,$value1,$value2,$value3...)

    Example-

    <?php
     $arr = array("hi", "hello");
     array_unshift($arr, "jo", "ko");
     print_r($arr);
    ?>

    Output-

    Array ( [0] => jo [1] => ko [2] => hi [3] => hello )

    44. array_values()

    his function will allow you to return all the values of a given array Syntax-

    array_values ( $array );

    Example-

    <?php
     $arr = array("hi", "hello");
     print_r(array_values($arr));
    ?> 

    Output-

    Array ( [0] => hi [1] => hello )

    45. array_walk()

    This function will allow you to apply a user function to each member of an array Syntax-

    array_walk ( $array, $funcname [, $parameter] );

    Example-

    <?php
     function func($value,$key) {
     echo "$key has the value $value \n";
     echo ("<br>");
     }
     $arr = array("a"=>"g", "b"=>"b", "c"=>"bye", "hi");
     array_walk($arr,"func");
    ?> 

    Output-

    a has the value g
    b has the value b
    c has the value bye
    0 has the value hi

    46. array_walk_recursive()

    This function will allow you to apply a user function recursively to each member of a given array Syntax-

    array_walk_recursive( $array, $funcname [,$parameter])

    Example-

    <?php
     function func($value,$key) {
     echo "$key has the value $value \n";
     echo ("<br>");
     }
     $arr1 = array("a"=>"g", "b"=>"b", "c"=>"bye", "hi");
     $arr2 = array($arr1, "d"=>"yellow", "e"=>"black");
     array_walk_recursive($arr2,"func");
    ?> 

    Output-

    a has the value g
    b has the value b
    c has the value bye
    0 has the value hi
    d has the value yellow
    e has the value black

    47. arsort()

    This function will allow you to sort an array in reverse order and maintain index association Syntax-

    arsort( $array [, $sort_flags] );

    Example-

    <?php
     $case = array("d"=>"hi", "a"=>"hello", "b"=>"bye" );
     arsort($case);
     print_r($case);
    ?> 

    Output-

    Array ( [d] => hi [a] => hello [b] => bye )

    48. asort()

    This function will allow you to sort an array and maintain index association Syntax-

    asort( $array [, $sort_flags] );

    Example-

    <?php
     $case = array("d"=>"hi", "a"=>"hello", "b"=>"bye" );
     asort($case);
     print_r($case);
    ?> 

    Output-

    Array ( [b] => bye [a] => hello [d] => hi )

    49. compact()

    This function will allow you to create the array containing variables and their values Syntax-

    compact($var1, $var2...);

    Example-

    <?php
     $val1 = "Hi";
     $val2 = "Hello";
     $val3 = "Bye";
     $val4 = compact("val1", "val2", "val3");
     print_r($val4);
    ?> 

    Output-

    Array ( [val1] => Hi [val2] => Hello [val3] => Bye )

    50. count()

    This function will allow you to count the elements in an array, or to count the properties in an object Syntax-

    count($array, $mode );

    Example-

     <?php
     $a[0] = "Hi";
     $a[1] = "Bye";
     $a[2] = "Hello";
     $result = count($a);
     print($result);
     ?> 

    Output- 3

    51. current()

    This function will return the current element in a given array Syntax-

    current ( $array );

    Example-

    <?php
     $subject = array('CS', 'CSS', 'OS', 'HTML');
     $val = current($subject); 
     print "$val <br />";
     $val = next($subject); 
     print "$val <br />";
     $val = current($subject);
     print "$val <br />";
     $val = prev($subject); 
     print "$val <br />";
     $val = end($subject);
     print "$val <br />";
     $val = current($subject); 
     print "$val <br />";
    ?> 

    Output-

    CS
    CSS
    CSS
    CS
    HTML
    HTML

    52. each()

    This function will return the current key and value pair from an array Syntax-

    each ( $array );

    Example-

    <?php
     $subject = array('CS', 'CSS', 'OS', 'HTML');
     $key_value = each($subject);
     print_r($key_value);
    ?> 

    Output-

    Array ( [1] => CS [value] => CS [0] => 0 [key] => 0 )

    53. end()

    This function will allow you to set the internal pointer of a given array to its last element Syntax-

    end ( $array );

    Example-

    <?php
     $subject = array('CS', 'CSS', 'OS', 'HTML');
     $key_value = end($subject);
     print_r($key_value);
    ?> 

    Output- HTML

    54. extract()

    This function will allow you to import the variables into the current symbol table from a given array. This function will take an associative array where the key values are specified as the variable names and the values are treated as the variable values. It will create a variable in the current symbol table for each given key/value pair. Syntax-

    extract($array, $extract_type, $prefix)

    Example-

    <?php
     $val = "large";
     $arr = array("color" => "blue", "size" => "medium", "shape" => "sphere");
     extract($arr, EXTR_PREFIX_SAME, "bbcx");
     echo "$color, $val, $shape;
    ?> 

    Output- blue, large, sphere

    55. in_array()

    This function will allow you to check if a specified value exists in an array Syntax-

    in_array ( $value, $array [,$strict ] );

    Example-

    <?php
     $subject = array("android", "java", "Linux");
     if (in_array("java", $subject)) {
     echo "Valid";
     }
     if (in_array("mac", $subject)) {
     echo "Invalid";
     }
    ?> 

    Output- Valid

    56. key()

    This function will allow you to fetch a key from a given array Syntax-

    key ( $array );

    Example-

    <?php
     $subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     while ($f_name = current($subject)) {
     if ($f_name == 'java') {
     echo key($subject)."\n";
     }
     next($subject);
     }
    ?>

    Output- b

    57. krsort()

    This function will allow you to sort an array by key in the reverse order Syntax-

    krsort ( $array, $sort_flag );

    Example-

    <?php
     $subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     krsort($subject);
     print_r($subject);
    ?> 
    
    Output-
    Array ( [c] => Linux [b] => java [a] => android )

    58. ksort()

    This function will allow you to sort an array by key Syntax-

    ksort ( $array, $sort_flag );

    Example-

    <?php
     $subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     ksort($subject);
     print_r($subject);
    ?> 

    Output-

    Array ( [a] => android [b] => java [c] => Linux )

    59. list()

    This function will allow you to assign the variables as if they were an array Syntax-

    list ( $var1, $var2, $var3.. )

    Example-

    <?php
     //$subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     $subject = array("android","java", "Linux");
     list($a, $b, $c) = $subject;
     echo "I have several fruits, a $a, a $b, and a $c.";
    ?> 

    Output- I have several fruits, a android, a java, and a Linux.

    60. natcasesort()

    This function will allow you to sort an array by using a case insensitive "natural order" algorithm Syntax-

    natcasesort ( $array );

    Example-

    <?php
     //$subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     $subject = array("android","java", "Linux");
     $array2 = $subject;
     natcasesort($array2);
     print_r($array2);
    ?> 

    Output-

    Array ( [0] => android [1] => java [2] => Linux )

    61. natsort()

    This function will allow you to sort an array using a "natural order" algorithm Syntax-

    natsort ( $array );

    Example-

    <?php
     //$subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     $subject = array("android","java", "Linux");
     $array2 = $subject;
    natsort($array2);
     print_r($array2);
    ?> 

    Output-

    Array ( [2] => Linux [0] => android [1] => java )

    62. next()

    This function will allow you to advance the internal array pointer of an array Syntax-

    next ( $array );

    Example-

    <?php
     //$subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     $subject = array("android","java", "Linux");
     $val = current($subject); 
     print "$val <br />";
     $val = next($subject);
     print "$val <br />";
    ?> 

    Output- android java

    63. pos()

    This function will allow you to create the Alias of current() Syntax-

    pos ( $array );

    Example-

    <?php
     //$subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     $subject = array("android","java", "Linux");
     $val = pos($subject); 
     print "$val <br />";
    ?> 

    Output- android

    64. prev()

    This function will allow you to rewind the internal array pointer Syntax-

    prev ( $array );

    Example-

    <?php
     //$subject = array('a' =>"android", 'b'=>"java", 'c'=>"Linux");
     $subject = array("android","java", "Linux");
     $val = current($subject);
     print "$val <br />";
    ?> 

    Output- android

    65. range()

    This function will allow you to create an array that has a range of elements Syntax-

    range ( $low, $high [, $step] );

    Example-

    <?php
     foreach (range(0, 6) as $num) {
     echo "$num, ";
     }
     print "\n";
     ?>

    Output-

    0, 1, 2, 3, 4, 5, 6,

    66. reset()

    This function will allow you to set the internal pointer of an array to its first element Syntax- reset ( $array );

    Example

    <?php
     $subject = array('cs', 'os', 'ds', 'ms');
     $var = reset($subject);
     print "$var <br />";
     $var = end($subject);
     print "$var <br />";
     ?>

    Output- cs ms

    67. rsort()

    This function will allow you to sort an array in the reverse order Syntax-

    rsort( $array [, $sort_flags] );

    Example-

    <?php
     $subject = array('cs', 'os', 'ds', 'ms');
     rsort($subject);
     print_r($subject);
     ?>

    Output-

    Array ( [0] => os [1] => ms [2] => ds [3] => cs )

    68. shuffle()

    This function will allow you to shuffles an array Syntax-

    shuffle( $array );

    Example-

    <?php
     $subject = array('cs', 'os', 'ds', 'ms');
     shuffle($subject);
     print_r($subject);
     ?>

    Output- Array ( [0] => ds [1] => ms [2] => os [3] => cs )

    69. sizeof()

    This function will allow you to create the Alias of count() Syntax-

    sizeof($array, $mode );

    Example-

    <?php
     $arr[0] = 1;
     $arr[1] = 3;
     $arr[2] = 5;
     $res = sizeof($arr);
     print($res);
    ?> 

    Output- 3

    70. sort()

    This function will sort a given array. Syntax-

    sort( $array [, $sort_flags] );

    Example-

    <?php
     $arr = array("d"=>"hi", "a"=>"hello", "b"=>"bye" );
     sort($arr);
     print_r($arr);
    ?> 

    Output-

    Array ( [0] => bye [1] => hello [2] => hi )

    71. uasort()

    This function will allow you to sort an array with a user-defined function and maintain index association Syntax-

    uasort ( $array, $cmp_function )

    Example-

    <?php
     function func($v1, $v2) {
     if ($v1 == $v2) return 0;
     return ($v1 > $v2) ? -1 : 1;
     }
     $arr = array("d"=>"hi", "a"=>"bye", "b"=>"hello" );
     uasort($arr, "func");
     print_r($arr);
    ?>

    Output-

    Array ( [d] => hi [b] => hello [a] => bye )

    72. uksort()

    This function will allow you to sorts an array by keys using a user-defined function Syntax

    uksort ( $array, $cmp_function )

    Example-

    <?php
     function func($v1, $v2) {
     if ($v1 == $v2) return 0;
     return ($v1 > $v2) ? -1 : 1;
     }
     $arr = array("d"=>"hi", "a"=>"bye", "b"=>"hello" );
     uksort($arr, "func");
     print_r($arr);
    ?>

    Output-

    Array ( [d] => hi [b] => hello [a] => bye )

    73. usort()

    This function will allow you to sort an array by values using a user-defined function Syntax

    usort ( $array, $cmp_function )

    Example-

    <?php
     function func($v1, $v2) {
     if ($v1 == $v2) return 0;
     return ($v1 > $v2) ? -1 : 1;
     }
     $arr = array("d"=>"hi", "a"=>"bye", "b"=>"hello" );
     usort($arr, "func");
     print_r($arr);
    ?>

    Output-

    Array ( [0] => hi [1] => hello [2] => bye )

    PHP array constants

    There are some PHP constants that are used while we work on the arrays. Some of the constants are mentioned below-

    • CASE_LOWER
    • CASE_UPPER
    • SORT_ASC
    • SORT_DESC
    • SORT_REGULAR
    • SORT_NUMERIC
    • SORT_STRING
    • SORT_LOCALE_STRING
    • COUNT_NORMAL
    • COUNT_RECURSIVE and many more.

    People are also reading: